package com.summer.service;

import com.alibaba.fastjson.JSON;
import com.summer.result.Result;
import com.summer.dao.CourseDao;
import com.summer.bean.Course;
import com.summer.dao.UserDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet({"/course/detail", "/course/selectCourse", "/course/enrolCourse", "/course/dropCourse"})
public class CourseService extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Result<Object> result = new Result<>();
        String path = request.getServletPath();
        if ("/course/detail".equals(path)) {
            result = doDetail(request, response);
        } else if ("/course/selectCourse".equals(path)) {
            result = doSelectCourse(request, response);
        }else if ("/course/enrolCourse".equals(path)){
            result = doEnrolCourse(request, response);
        } else if ("/course/dropCourse".equals(path)) {
            result = doDropCourse(request, response);
        }
        if (result != null) {
            String jsonResult = JSON.toJSONString(result);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print(jsonResult);
            out.flush();
            out.close();
        }
    }

    public Result doDropCourse(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        System.out.println(request.getSession().getAttribute("username").toString());
//        System.out.println("doDropCourse");
        response.setContentType("application/json;charset=UTF-8");
        String username = request.getParameter("username").toString();
        String cno = request.getParameter("cno");
        CourseDao.dropCourse(username, cno);
        return Result.success();
    }

    private Result<Object> doEnrolCourse(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//        System.out.println(request.getSession().getAttribute("username").toString());
//        System.out.println("doEnrolCourse");
        response.setContentType("application/json;charset=UTF-8");
        String username = request.getParameter("username");
        if(username == null){;
            return Result.error("请先登录");
        }
        String cno = request.getParameter("cno");
        int res = CourseDao.selectEnrol(username, cno);
        if(res == 0){
            String sno = UserDao.selectByUsername(username).getSno();
            if(sno == null){
                return Result.error("请先绑定学号");
            }else{
                CourseDao.enrolCourse(username, cno);
            }
        }
        return Result.success();
    }

    private Result<Object> doSelectCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        response.setContentType("application/json;charset=UTF-8");
        String flag = request.getParameter("flag");
        String username = request.getParameter("username");
        List<Course> courses;
        if(flag.equals("true")){
            String sno = UserDao.selectByUsername(username).getSno();
            if(sno == null){;
                return Result.error("未绑定学号，请绑定学号后继续操作");
            }else{
                courses = CourseDao.selectCourseByUsername(username);
            }
        }else{
            courses = CourseDao.selectAllCourse();
        }
        return Result.success(courses);
    }

    private Result doDetail(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        response.setContentType("application/json;charset=UTF-8");
        if(id.substring(0, 6).equals("detail")){
            String courseDetail = CourseDao.getCourseDetailById(Integer.parseInt(id.substring(id.length()-1)));
            return Result.success(courseDetail);
        } else if(id.substring(0, 8).equals("relative")){
            List<Course> courses = CourseDao.GetRelativeDetailById(Integer.parseInt(id.substring(id.length()-1)));
            return Result.success(courses);
        }

        return null;
    }
}
